import { ref, computed } from 'vue';
import { defineStore } from 'pinia';

export const counterStore = defineStore({
	id: 'counter', //模块名
	state: () => {
		return {
			num: 0,
			name: '张三',
			age: 18
		};
	},
	getters: {
		changeNum() {
			console.log('getters');
			return this.num + 1000;
		}
	},
	actions: {
		upNum(val) {
			this.num = val;
		}
	},

	//开启数据缓存(pinia)
	persist: {
		enabled: true,
		// 默认所有 state 都会进行缓存，你能够通过 paths 指定要长久化的字段，其余的则不会进行长久化。
		strategies: [
			{
				key: 'counter', //本地化持久存储,不加就是会话存储
				storage: localStorage,
				paths: ['name', 'num'] //存储字段
			}
		]
	}
});
